<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 拜访记录接口
 */
class Visitlog extends Api
{

    protected $noNeedLogin = ['visitnotice'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Visitlog();

    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post('row/a');
            $formId = $this->request->post('form_id');
            if ($params)
            {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill)
                {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $params['is_mark'] = 1;
                    $user = $this->auth->getUserinfo();
                    $waylist = $this->model->getWayList();
                    $params['record'] = $waylist[$params['way']] . '：' . $user['nickname'] . '：' . $params['remark'];
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false)
                    {

                        if($params['notice_time']){
                            $redis = new \Predis\Client(\think\Config::get('redis'));
                            $key = "visitlogNotice:";
                            $value = $result->id. '-' .$formId;
                            $redis->lpush($key,$value);
                        }
                        $this->success();
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

    }

    public function otherdata(){

        $way = one_array_two($this->model->getWayList());

        $this->success('',['way' => $way]);

    }

    public function visitnotice(){
        $redis = new \Predis\Client(\think\Config::get('redis'));
        $wx = new \wechat\Wx();
        $key = "visitlogNotice:";
        $list = $redis->lrange($key,0,-1);
        $arr = [];
        foreach($list as $k => $v){
            $condition = $v['notice_time'] -time();
//            if($condition >= 1700 && $condition <=1900){
//                $arr = explode('-',$v);
//                $wx->sendWxMsg($v,$arr[0],$arr[1]);
//            }

            $arr = explode('-',$v);
            $wx->sendWxMsg($v,$arr[0],$arr[1]);
        }
    }

    /**
     * 发送名片留言
     */
    public function cardLeaveMsg(){

        $from_userid = $this->request->request('from_userid/d');
        $content = $this->request->request('content');
        $redis = new \Predis\Client(\think\Config::get('redis'));

        $wx = new \wechat\Wx();
        $key = "cardLeaveMsg:" . $from_userid;
        $arr = [
            'from_userid' => $from_userid,
            'msg_userid' => $this->auth->id,
            'content' => $content,
            'createtime' => time()
        ];
        $redis->lpush($key,json_encode($arr));
        $this->success('发布成功');
    }

    /**
     * 名片留言页面
     */
    public function msg(){
        $from_userid = $this->request->request('from_userid');
        $fromUser = \app\common\model\User::get($from_userid);

        $redis = new \Predis\Client(\think\Config::get('redis'));
        $wx = new \wechat\Wx();
        $key = "cardLeaveMsg:" . $from_userid;
        $list = $redis->lrange($key,0,100);
        foreach($list as $k => $v){
            $v = json_decode($v);

        }
        $data = [
            'from_user' => [
                'id' => $fromUser['id'],
                'nickname' => $fromUser['nickname'],
                'avatar' => $fromUser['avatar']
            ],
            'msg_list' => $list
        ];
        $this->success('',$data);
    }


}